<?php
class AppController extends Controller {
  public $components = array(
    'Acl',
    'Auth' => array(
      'authorize' => 'actions',
      'loginRedirect' => array(
        'admin' => false,
        'controller' => 'users',
        'action' => 'dashboard'
      ),
      'loginError' => 'Thông tin tài khoản không đúng.',
      'authError' => 'Bạn không thể truy xuất được phần này. Vui lòng đăng nhập hoặc đăng ký với chúng tôi'
    ),
  'Session'   
  );
  
  function beforeFilter() {
    $user = $this->Auth->user();
    if (!empty($user)) {
      Configure::write('User', $user[$this->Auth->getModel()->alias]);
    }
    Configure::write('Config.language', 'eng');
    $this->Session->write('Config.language', 'eng');
    
    
    
    
  }
  
  public function beforeRender() {
    $user = $this->Auth->user();
    Controller::loadModel('Group');
    Controller::loadModel('City');    
    
    if (!empty($user)) {
      $user = $user[$this->Auth->getModel()->alias];
      $group = $this->Group->find('first', array('conditions' => array('Group.id'=>$this->Auth->user('group_id'))));
    }
    
    //current city
    $c_city = array();
    if($this->Session->read("c_city_id") != '')
    {      
      $c_city = $this->City->find('first', array('conditions'=>array('City.id'=>$this->Session->read("c_city_id"))));
    }
    
    //current city
    $c_year = date('Y');
    if($this->Session->read("c_year") != '')
    {      
      $c_year = $this->Session->read("c_year");
    }
    else
    {
      $this->Session->write("c_year", $c_year);
    }
    
    //current quarter
    $c_quarter = round(date('m')/3, 0)+1;
    //echo $c_quarter;
    if($this->Session->read("c_quarter") != '')
    {      
      $c_quarter = $this->Session->read("c_quarter");
    }
    else
    {
      $this->Session->write("c_quarter", $c_quarter);
    }
    //$this->Session->write("c_quarter", round(date('m')/3, 0)+1);
    
    //$this->Session->write("city_id", "2");
    $c_cities['tb'] = $this->City->find('all', array('order'=>'City.name','conditions'=>array('City.area'=>'TB')));
    $c_cities['tn'] = $this->City->find('all', array('order'=>'City.name','conditions'=>array('City.area'=>'TN')));
    $c_cities['dnb'] = $this->City->find('all', array('order'=>'City.name','conditions'=>array('City.area'=>'DNB')));
    $c_cities['tnb'] = $this->City->find('all', array('order'=>'City.name','conditions'=>array('City.area'=>'TNB')));
    
    //$this->redirect(array('controller'=>'users', 'action'=>'offline', 'admin' => false));
    //var_dump($user);
    //check time to update
    if(
        ((date('m') == 4 && strtotime(date('Y-m-d H:i:s')) >= strtotime(date(date('Y').'-03-15 00:00:00')) && strtotime(date('Y-m-d H:i:s')) <= strtotime(date(date('Y').'-04-01 00:00:00')))
          || (date('m') == 6 && strtotime(date('Y-m-d H:i:s')) >= strtotime(date(date('Y').'-06-15 00:00:00')) && strtotime(date('Y-m-d H:i:s')) <= strtotime(date(date('Y').'-07-01 00:00:00')))
          || (date('m') == 9 && strtotime(date('Y-m-d H:i:s')) >= strtotime(date(date('Y').'-09-15 00:00:00')) && strtotime(date('Y-m-d H:i:s')) <= strtotime(date(date('Y').'-10-01 00:00:00')))
          || (date('m') == 12 && strtotime(date('Y-m-d H:i:s')) >= strtotime(date(date('Y').'-12-15 00:00:00')) && strtotime(date('Y-m-d H:i:s')) <= strtotime(date(date('Y').'-12-31 59:59:59')))
        ) || $user["id"] == 1
      )
    {
      $isActive = true;
      $this->Session->write("isActive", true);
    }
    else
    {
      $isActive = false;
      $this->Session->write("isActive", false);
    }
    
    $isActive = true;
    $this->Session->write("isActive", true);
    
    //$this->Session->write("c_quarter", 4);
    
    //echo date('m');
    
    $this->set(compact('isActive'));
    $this->set(compact('user'));
    $this->set(compact('group'));
    $this->set(compact('c_city'));
    $this->set(compact('c_cities'));
    $this->set(compact('c_year'));
    $this->set(compact('c_quarter'));
  }
}
?>